import jsonp from 'common/js/jsonp.js'
import axios from 'axios'
import { commonParams } from './config'

export function getLyric(mid) {
  const url = '/apimusic/qqyylyric'

  const data = Object.assign({}, commonParams, {
    songmid: mid,
    g_tk: '1819849191',
    platform: 'yqq',
    needNewCode: 0,
    categoryId: 10000000,
    pcachetime: +new Date(),
    jsonpCallback: 'MusicJsonCallback_lrc',
    loginUin: 0,
    hostUin: 0,
    format: 'jsonp',
    notice: 0
  })

  return axios.get(url, {
    params: data
  }).then((res) => {
    return Promise.resolve(res.data)
  })
}

// 获取歌曲播放的源 ip地址，防止IP地址变更
export function getPlayUrlIp() {
  const url = 'https://u.y.qq.com/cgi-bin/musicu.fcg'
  /* eslint-disable */
  let randCallbackName = '__jp' + String(Math.random()).slice(2,8)

  let params = Object.assign({}, commonParams, {

    callback: randCallbackName,
    jsonpCallback: randCallbackName,
    g_tk: 1819849191,
    loginUin: 0,
    hostUin: 0,
    format: 'jsonp',
    notice: '0',
    platform: 'yqq',
    needNewCode: 0,
    data: '{"req":{"module":"CDN.SrfCdnDispatchServer","method":"GetCdnDispatch","param":{"guid":"3510444360","calltype":0,"userip":""}},"req_0":{"module":"vkey.GetVkeyServer","method":"CgiGetVkey","param":{"guid":"3510444360","songmid":["001Qu4I30eVFYb"],"songtype":[0],"uin":"0","loginflag":1,"platform":"20"}},"comm":{"uin":0,"format":"json","ct":20,"cv":0}}'
  })
  return jsonp(url, params, { name: randCallbackName })
}

// 获取歌曲播放地址列表
export function getNewUrl(songmid) {
  const url = 'https://u.y.qq.com/cgi-bin/musicu.fcg'
  /* eslint-disable */
  let randCallbackName = '__jp' + String(Math.random()).slice(2,8)

  let params = Object.assign({}, commonParams, {
    // g_tk: '1819849191',
    callback: randCallbackName,
    jsonpCallback: randCallbackName,
    loginUin: 0,
    hostUin: 0,
    format: 'jsonp',
    notice: '0',
    platform: 'yqq',
    needNewCode: '0',
    data:'{"req_0":{"module":"vkey.GetVkeyServer","method":"CgiGetVkey","param":{"guid":"3510444360","songmid":["'+songmid+'"],"songtype":[0],"uin":"0","loginflag":1,"platform":"20"}},"comm":{"uin":0,"format":"json","ct":20,"cv":0}}'
  })
  return jsonp(url, params, { name: randCallbackName })
}
